<!DOCTYPE html>
<html lang="en">

<head>
    <meta charset="utf-8">
    <meta name="viewport" content="maximum-scale=1.0,minimum-scale=1.0,user-scalable=0,width=device-width,initial-scale=1.0"
    />
    <title>单例模式</title>
</head>

<body>
    <script type="text/javascript">
        function SingleInstance(name) {
            this.name = name
            this.age = 18
        }
        SingleInstance.prototype = {
            say: function () {
                console.log("say hello")
            }
        }
        SingleInstance.getInstance = (function () {
            console.log(this)
            var instance = null
            return function(name){
                if (!instance) {
                    instance = new SingleInstance(name)
                }
                return instance
            }
        })()

        var test1 = SingleInstance.getInstance()
        var test2 = SingleInstance.getInstance()
        console.log(test1 === test2)
    </script>

</body>

</html>